<template>
  <div>
    <el-card class="box-card" style="height:800px">
      <div slot="header" class="clearfix">
        <span>
          <navigation />
        </span>
      </div>
      <div class="list" id="taskScore">
        <el-scrollbar class="page-component__scroll" style="height:100%">
          <div style="height:700px;">
            <el-table
              :header-cell-style="{ background: '#f5f5f5' }"
              class="perTabSty"
              :data="tableData"
              style="width: 100%;margin-bottom: 20px;"
              row-key="id"
              border
              height="680px"
              default-expand-all
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
              <el-table-column
                label="序号"
                width="100"
                prop="index"
                align="center"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                label="重点工作名称"
              ></el-table-column>
              <el-table-column
                align="center"
                prop="departmentName"
                label="领导小组"
              ></el-table-column>

              <el-table-column label="操作时间" width="180">
                <template slot-scope="scope" v-if="scope.row.tscAuditorName">
                  {{ timestampToTimes(scope.row.updateTime) }}
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="tscAuditorName"
                label="申请人"
                width="100"
              ></el-table-column>
              <el-table-column label="操作" align="center" width="250">
                <template
                  slot-scope="scope"
                  v-if="!scope.row.children || scope.row.children.length == 0"
                >
                  <el-button type="text" @click="handleLook(scope.row)"
                    >查看</el-button
                  >
                  <el-button
                    type="text"
                    @click="handFileList(scope.row)"
                    v-if="scope.row.isDisplay == true"
                    >附件列表</el-button
                  >
                  <el-button type="text" @click="audit(scope.row)"
                    >申请审核</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-scrollbar>
      </div>
      <!-- 附件列表弹框 -->
      <el-dialog
        width="60%"
        title="附件列表配置"
        :visible.sync="dialogFormVisible"
        :close-on-click-modal="false"
        @closed="closeDialog"
      >
        <el-form
          :inline="true"
          label-width="95px"
          :model="dutyFrom"
          :hide-required-asterisk="false"
        >
          <el-form-item label="附件：" prop="file" class="boxCount">
            <el-upload
              class="upload-demo"
              :action="uploadUrl"
              :on-success="successFile"
              :on-remove="handleDealFileRemove"
              :file-list="fileList"
              multiple
              ref="rebateUpload"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button
              size="small"
              class="saveBtn"
              type="primary"
              @click="submitForm"
              >添加</el-button
            >
          </el-form-item>
        </el-form>
        <el-table :data="fileTableData" border style="width: 100%">
          <el-table-column prop="file" label="附件名">
            <template slot-scope="scope">
              <a :href="returnFile(scope.row.fileName)">{{
                scope.row.fileName
              }}</a>
            </template>
          </el-table-column>
          <el-table-column label="上传时间">
            <template slot-scope="scope">{{ scope.row.addTime }}</template>
          </el-table-column>

          <el-table-column label="上传人" prop="userName"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="removeFile(scope.row)" type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <el-dialog
        title="选择季度"
        :visible.sync="showAddFormdialog"
        width="400px"
        @close="cancelForm('taskRef')"
      >
        <el-form ref="taskRef" :model="taskForm" label-width="60px">
          <el-form-item label="季度:" prop="quarter">
            <el-select
              class="userIntSize"
              v-model="taskForm.quarter"
              clearable
              @change="quarterChange"
              placeholder="请选择"
            >
              <el-option
                v-for="item in quarterList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              style="width:70%"
              type="primary"
              @click="submitForms('taskRef')"
              >确认</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import configJson from "../../api/config";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      uploadUrl: configJson.API_URL + "fileController/uploadFile",
      dutyFrom: {
        id: "",
        aimsId: "",
        year: "2020",
        fileQuarter: "",
        files: [],
        type: "2"
      },
      fileList: [],
      taskFlag: "",
      quarterList: [
        { id: 1, name: "第一季度" },
        { id: 2, name: "第二季度" },
        { id: 3, name: "第三季度" },
        { id: 4, name: "第四季度" }
      ],
      fileTableData: [],
      fileOption: [],
      fileTypeList: [],
      fileStrList: [],
      showAddFormdialog: false,
      taskForm: {
        departmentId: "",
        type: 1,
        quarter: "",
        id: "",
        status: ""
      },
      taskIdData: [],
      arrData: [],
      arrData1: []
    };
  },
  mounted() {
    this.getTaskList();
    this.getFileList();
    this.getFileType();
  },
  methods: {
    // 时间转换
    timestampToTimes(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear();
      var M =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var D = date.getDate() + " ";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      // return Y + M + D;
      let _time = `${Y}年${M}月${D}日`;
      return _time;
    },
    returnFile(val) {
      return `${configJson.API_URL}/fileController/${val}`;
    },
    transLate(val) {
      let text = "";
      switch (val) {
        case 1:
          text = "第一季度";
          break;
        case 2:
          text = "第二季度";
          break;
        case 3:
          text = "第三季度";
          break;
        default:
          text = "第四季度";
      }
      return text;
    },
    successFile(response, file, fileList) {
      if (response.code == 200) {
        this.fileList = fileList;
        console.log(this.fileList);
        let fileUrlStr = this.uploadUrl.split("/");
        let fileRemoveChl = fileUrlStr.slice(0, fileUrlStr.length - 1);
        this.fileUrlEnd = fileRemoveChl.join("/");
        this.fileName = this.fileUrlEnd + "/" + response.data;
        this.dutyFrom.files.push(response.data); //获取上传文件名
      }
    },
    handleDealFileRemove(file, fileList) {
      this.fileList = fileList;
      this.fileName = "";
    },
    audit(row) {
      this.taskIdData = row;
      this.showAddFormdialog = true;
      this.taskForm.departmentId = row.departmentId;
    },
    quarterChange(val) {
      let str = {
        taskId: `${this.taskIdData.id}`,
        departmentTypeId: null,
        time: val
      };
      this.$axios
        .post("deptTask/getDeptQuarterExamine", str)
        .then(res => {
          if (res.code == 200) {
            console.log(res);
            // res.data.forEach(i => {
            //   if (val == 1) {
            //     this.arrData.push(i.sc1Id);
            //     this.arrData1.push(i.sc1Id);
            //   } else if (val == 2) {
            //     this.arrData.push(i.sc2Id);
            //     this.arrData1.push(i.sc2Id);
            //   } else if (val == 3) {
            //     this.arrData.push(i.sc3Id);
            //     this.arrData1.push(i.sc3Id);
            //   } else if (val == 4) {
            //     this.arrData.push(i.sc4Id);
            //     this.arrData1.push(i.sc4Id);
            //   }
            // });
            this.arrData = res.data;
            this.quarterChangeTow(val);
            console.log(this.arrData);
            // this.setstauas();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    quarterChangeTow(val) {
      let str = {
        taskId: `${this.taskIdData.id}`,
        departmentTypeId: null,
        time: val
      };
      this.$axios
        .post("/deptTask/getDeptQuarterIds", str)
        .then(res => {
          if (res.code == 200) {
            console.log(res);

            // res.data.forEach(i => {
            //   if (val == 1) {
            //     this.arrData.push(i.sc1Id);
            //     this.arrData1.push(i.sc1Id);
            //   } else if (val == 2) {
            //     this.arrData.push(i.sc2Id);
            //     this.arrData1.push(i.sc2Id);
            //   } else if (val == 3) {
            //     this.arrData.push(i.sc3Id);
            //     this.arrData1.push(i.sc3Id);
            //   } else if (val == 4) {
            //     this.arrData.push(i.sc4Id);
            //     this.arrData1.push(i.sc4Id);
            //   }
            // });
            this.arrData = res.data;
            this.arrData1 = res.data;
            this.setstauas();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    setstauas() {
      if (this.arrData.length == 0) {
        this.$message.error("当前选项不能申请审核，请先进行打分操作");
        return;
      }
      let obj = {
        id: this.arrData[0],
        type: 2,
        time: this.taskForm.quarter
      };
      this.$axios
        .post("deptScore/load", obj)
        .then(res => {
          if (res.code == 200) {
            if (res.data) {
              this.taskForm.status = res.data.status;
            } else {
              this.taskForm.id = "";
              this.taskForm.status = "";
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    submitForms() {
      if (this.arrData == false) {
        this.$message({
          message: "当前选项不能申请审核，请先进行打分操作",
          type: "error"
        });
        return;
      }
      if (this.taskForm.status == 1) {
        this.$message.error("待审核状态,不能重复申请");
      } else if (this.taskForm.status == 2) {
        this.$message.error("已通过状态,不能再次申请");
      } else {
        this.auditing();
      }

      setTimeout(() => {
        this.showAddFormdialog = false;
      }, 500);
    },
    auditing() {
      this.$axios
        .post("deptScore/auditing", {
          ids: this.arrData1,
          status: 1,
          taskId: this.taskIdData.id,
          year: 2020,
          quarter: this.taskForm.quarter
        })
        .then(res => {
          if (res.code == 200) {
            if (res.data) {
              this.$message({
                message: "申请成功",
                type: "success"
              });
              this.getTaskList();
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    cancelForm() {
      this.taskForm = {
        departmentId: "",
        type: 2,
        quarter: "",
        id: ""
      };
    },
    getFileType() {
      this.$axios
        .post("fileLibTypeController/getList")
        .then(res => {
          if (res.code == 200) {
            console.log(res);
            this.fileTypeList = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    fileLists(arr) {
      let str = arr.split("/");
      return str.pop();
    },
    // 获取任务列表
    getTaskList() {
      this.$axios
        .post("task/getList")
        .then(res => {
          if (res.code == 200) {
            this.tableData = res.data;
            let _taskId = localStorage.getItem("taskId");
            this.tableData.forEach((item, index) => {
              if (_taskId == "null" || _taskId == "undefined") {
                item.isDisplay = true;
                if (item.children || scope.row.children.length != 0) {
                  item.children.forEach((i, index2) => {
                    i.isDisplay = true;
                    i.index = `${index + 1}.${index2 + 1}`;
                  });
                }
                item.index = index + 1;
              } else {
                item.index = index + 1;
                if (item.id == _taskId) {
                  item.isDisplay = true;
                } else {
                  item.isDisplay = false;
                }
              }
            });
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 获取附件列表
    getFileList() {
      this.$axios
        .post("fileController/getList")
        .then(res => {
          if (res.code == 200) {
            this.fileOption = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 查看
    handleLook(row) {
      console.log(row, "row");
      this.$router.push({
        path: "/unitScore",
        query: { id: row.id, name: row.name }
      });
    },
    // 打开附件列表弹框
    handFileList(row) {
      this.dialogFormVisible = true;
      this.taskFlag = row.id;
      this.getTabData();
    },
    getTabData() {
      this.$axios
        .post(
          "/taskFile/getList",
          { taskId: this.taskFlag, type: this.dutyFrom.type },
          2
        )
        .then(res => {
          if (res.code == 200) {
            this.fileTableData = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 选择年
    changeYear(val) {
      this.dutyFrom.year = val.toLocaleDateString();
    },
    submitForm() {
      if (this.dutyFrom.files.length == 0) {
        this.$message.error("请选择附件");
        return;
      }

      let obj = {
        aimsId: this.taskFlag,
        year: this.dutyFrom.year.substring(0, 4),
        fileQuarter: this.dutyFrom.fileQuarter,
        files: this.dutyFrom.files,
        type: this.dutyFrom.type
      };
      this.$axios
        .post("fileController/addFile", obj)
        .then(res => {
          if (res.code == 200) {
            this.$message({
              message: "添加成功",
              type: "success"
            });
            this.dutyFrom.fileQuarter = "";
            this.dutyFrom.files = [];
            this.dutyFrom.id = "";
            this.fileList = [];
            this.getTabData();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    removeFile(row) {
      this.$confirm("确认删除？")
        .then(_ => {
          this.$axios
            .post("taskScoreFile/remove", { id: row.id }, 2)
            .then(res => {
              if (res.code == 200) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
                this.getTabData();
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(_ => {});
    },
    closeDialog() {
      this.dutyFrom = {
        year: "",
        quarter: "",
        file: "",
        fileType: ""
      };
    },
    fileChange(val) {
      this.$axios
        .post("fileController/getList", { type: val }, 2)
        .then(res => {
          if (res.code == 200) {
            this.fileStrList = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style scoped>
.saveBtn {
  margin-left: 50px;
}
</style>
